import { userStore } from '../stores/UserStore';
import { Ajax } from '../utils/http';

export class SubscribeService {
    private id: string;
    constructor(_id: string,) {
        this.id = _id;
    }

    public async send(): Promise<PayData> {
        let response: PayData;
        let data = {
            'priceid': this.id
        }
        try {
            response = await new Ajax<PayData>('/order/createCheckSession', data).post();
        } catch (e) {
            throw e;
        }
        return response;
    }
}

export class SubscribeListService {
    public async send(): Promise<Array<SubscribeItem>> {
        let response: Array<SubscribeItem>;

        try {
            response = await new Ajax<Array<SubscribeItem>>('/order/packageList').post();
        } catch (e) {
            throw e;
        }
        return response;
    }
}

export class CancelSubscribeService {
    // private id: string;
    // constructor(_id: string,) {
    //     this.id = _id;
    // }

    public async send() {
        let response;
        let data = {
            'subscription': userStore.userInfo?.subscription,
            'customer': userStore.userInfo?.customer
        }
        try {
            response = await new Ajax('/order/cancelRenew', data).post();
        } catch (e) {
            throw e;
        }
        return response;
    }
}


export class ActivateSubscribeService {
    private id: string;
    constructor(_id: string,) {
        this.id = _id;
    }

    public async send() {
        let response;
        let data = {
            'subscription': userStore.userInfo?.subscription,
            'customer': userStore.userInfo?.customer,
            'priceid': this.id
        }
        try {
            response = await new Ajax('/order/updateSubscribe', data).post();
        } catch (e) {
            throw e;
        }
        return response;
    }
}

export interface PayData {
    id: string;
    object: string;
    amountSubtotal: number;
    amountTotal: number;
    cancelUrl: string;
    currency: string;
    livemode: boolean;
    mode: string;
    paymentMethodTypes: string[];
    paymentStatus: string;
    successUrl: string;
    url: string;
}


export interface SubscribeItem {
    id: string;
    priceid: string;
    name: string;
    price: string;
    desc: string;
    rules: string[];
    duration: string;
    createtime: string;
    updatetime: string;
    status: string;
    isSubscribe: number;
    isRenew: number;
}


